package com.chase.springboot.controller;

import com.chase.springboot.common.api.ApiResult;
import com.chase.springboot.model.dto.CommentDTO;
import com.chase.springboot.model.entity.Comment;
import com.chase.springboot.model.entity.User;
import com.chase.springboot.model.vo.CommentVO;
import com.chase.springboot.service.ICommentService;
import com.chase.springboot.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.chase.springboot.jwt.JwtUtil.USER_NAME;


@RestController
@RequestMapping("/comment")
public class CommentController extends BaseController {

    @Resource
    private ICommentService bmsCommentService;
    @Resource
    private IUserService umsUserService;

    @GetMapping("/get_comments")
    public ApiResult<List<CommentVO>> getCommentsByTopicID(@RequestParam(value = "topicId", defaultValue = "1") String topicId) {
        List<CommentVO> lstBmsComment = bmsCommentService.getCommentsByTopicID(topicId);
        System.out.println(topicId + " " + lstBmsComment);
        return ApiResult.success(lstBmsComment);
    }
    @PostMapping("/add_comment")
    public ApiResult<Comment> add_comment(@RequestHeader(value = USER_NAME) String userName,
                                          @RequestBody @Valid CommentDTO dto) {
        if (dto.getReplyId() == null){
            dto.setReplyId("0");
        }
        User user = umsUserService.getUserByUsername(userName);
        Comment comment = bmsCommentService.create(dto, user);
        return ApiResult.success(comment);
    }
    @GetMapping("/get_first_layer_comment")
    public ApiResult<List<CommentVO>> getFirstLayerComments(@RequestParam(value = "topicId", defaultValue = "1") String topicId){
        List<CommentVO> comments = bmsCommentService.getFirstLayerComments(topicId);
        return ApiResult.success(comments);
    }
    @GetMapping("/get_comment_by_commentid")
    public ApiResult<List<CommentVO>> getCommentsByCommentId(@RequestParam(value = "commentId", defaultValue = "1") String commentId){
        List<CommentVO> comments = bmsCommentService.getCommentsByCommentId(commentId);
        return ApiResult.success(comments);
    }

    @DeleteMapping("/{commentId}")
    public ApiResult deleteComment(@PathVariable("commentId") String commentId){
        System.out.println(commentId);
        boolean b = bmsCommentService.deleteCommentAndChilds(commentId);
        return ApiResult.success(null,"删除成功");
    }
}
